package com.hualude.roadsign.controller;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.service.*;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/10/13.
 */

@Controller
@RequestMapping("/rds_alarmManage")
public class AlarmController {

    @Resource
    private AlarmService alarmService;
    @Resource
    private SignalLampManageService signalLampManageService;
    @Resource
    private SignalMachineManageService signalMachineManageService;
    @Resource
    private SolarEnergyManageService solarEnergyManageService;
    @Resource
    private HighLeverManageService highLeverManageService;
    @Resource
    private MonitorService monitorService;

    @ResponseBody
    @RequestMapping("/getAlarm4DT")
    public DTResponseDto getAlarm4DT(DTRequestDto dtRequestDto, Integer orgId, Integer actorId, Integer deviceType) throws Exception {
        DTResponseDto dtResponseDto = new DTResponseDto();
        try {
            dtResponseDto = alarmService.getAlarm4DT(dtRequestDto, orgId, actorId, deviceType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }

    @ResponseBody
    @RequestMapping("/getAlarmBySearch4DT")
    public DTResponseDto getAlarmBySearch4DT(DTRequestDto dtRequestDto, String orgId, String deviceId, String createTime, Integer deviceType) throws Exception {
        DTResponseDto dtResponseDto = new DTResponseDto();
        try {
            dtResponseDto = alarmService.getAlarmBySearch4DT(dtRequestDto, orgId, deviceId, createTime, deviceType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }

    @ResponseBody
    @RequestMapping("/getPortById")
    public JsonMsgDto getPortById(String deviceId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.getPortById(deviceId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/deletePortById")
    public JsonMsgDto deletePortById(Integer portId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.deletePortById(portId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/addPort4Alarm")
    public JsonMsgDto addPort4Alarm(PortDto portDto) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.addPort4Alarm(portDto));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    @ResponseBody
    @RequestMapping("/updatePort")
    public JsonMsgDto updatePort(PortDto portDto) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.updatePort(portDto));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/addAlarm")
    public JsonMsgDto addAlarm(AlarmDto alarmDto) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.addAlarm(alarmDto));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/updateAlarm")
    public JsonMsgDto updateAlarm(AlarmDto alarmDto) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.updateAlarm(alarmDto));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/deleteAlarmById")
    public JsonMsgDto deleteAlarmById(String alarmIds) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.deleteAlarmById(alarmIds));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/sendIoCmd")
    public JsonMsgDto sendIoCmd(String deviceId, String cmd, Integer cycleTime) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.sendIoCmd(deviceId, cmd, cycleTime));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/getDeviceRecord4DT")
    public DTResponseDto getDeviceRecord4DT(DTRequestDto dtRequestDto, String deviceId, String infoType) throws Exception {
        DTResponseDto dtResponseDto = new DTResponseDto();
        try {
            dtResponseDto = alarmService.getDeviceRecord4DT(dtRequestDto, deviceId, infoType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }

    @ResponseBody
    @RequestMapping("/getAlarmPath")
    public JsonMsgDto getAlarmPath(String deviceId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.getAlarmPath(deviceId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/getAlarmList")
    public JsonMsgDto getAlarmList(Integer orgId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            jsonMsgDto.setJsonData(alarmService.getAlarmList(orgId));
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    /**
     * @Description:
     * @Author: WYX.
     * @Date: 2018/5/14 15:04.
     */
    @ResponseBody
    @RequestMapping("/getDeviceIdList")
    public DTResponseDto getDeviceIdList(DTRequestDto dtRequestDto,
                                         String actorId,
                                         Integer orgId,
                                         Boolean audit,
                                         String startRoad,
                                         String endRoad,
                                         String roadName,
                                         Integer alarmBinding,
                                         Integer deviceType) {
        DTResponseDto dtResponseDto = new DTResponseDto();
        try {
            switch (deviceType) {
                case 2:
                    dtResponseDto = signalLampManageService.getSignalLamp4DT(
                            dtRequestDto, actorId, orgId, audit, startRoad, endRoad, alarmBinding);
                    break;
                case 3:
                    dtResponseDto = signalMachineManageService.getSignalMachine4DT(
                            dtRequestDto, actorId, orgId, audit, startRoad, endRoad, alarmBinding);
                    break;
                case 4:
                    dtResponseDto = solarEnergyManageService.getSolarEnergy4DT(
                            dtRequestDto, actorId, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
                    break;
                case 5:
                    dtResponseDto = highLeverManageService.getHighLever4DT(
                            dtRequestDto, actorId, orgId, audit, startRoad, endRoad, roadName,alarmBinding);
                    break;
                case 8:
                    dtResponseDto = monitorService.getMonitor4DT(
                            dtRequestDto, actorId, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }

    /**
     * @Description:
     * @Author: WYX.
     * @Date: 2018/5/19 17:03.
     */
    @ResponseBody
    @RequestMapping("/boundingDevice")
    public JsonMsgDto boundingDeviceId(Integer deviceId, Integer deviceType, String alarmDeviceId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            if (deviceType == 2) {
                jsonMsgDto.setJsonData(signalLampManageService.bindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 3) {
                jsonMsgDto.setJsonData(signalMachineManageService.bindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 4) {
                jsonMsgDto.setJsonData(solarEnergyManageService.bindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 5) {
                jsonMsgDto.setJsonData(highLeverManageService.bindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 8) {
                jsonMsgDto.setJsonData(monitorService.bindingDevice(alarmDeviceId, deviceId));
            }

        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/unboundingDevice")
    public JsonMsgDto unboundingDevice(Integer deviceId, Integer deviceType, String alarmDeviceId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setIsSuccess(true);
            if (deviceType == 2) {
                jsonMsgDto.setJsonData(signalLampManageService.unbindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 3) {
                jsonMsgDto.setJsonData(signalMachineManageService.unbindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 4) {
                jsonMsgDto.setJsonData(solarEnergyManageService.unbindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 5) {
                jsonMsgDto.setJsonData(highLeverManageService.unbindingDevice(alarmDeviceId, deviceId));
            } else if (deviceType == 8) {
                jsonMsgDto.setJsonData(monitorService.unbindingDevice(alarmDeviceId, deviceId));
            }
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/getLastAlarmPushByDeviceId")
    public JsonMsgDto getLastAlarmPushByDeviceId(String deviceId) throws Exception {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(alarmService.getLastAlarmPushByDeviceId(deviceId));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @ResponseBody
    @RequestMapping("/getDeviceInfo")
    public JsonMsgDto getDeviceInfo(Integer deviceType, Integer deviceId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            if (deviceType == 2) {
                jsonMsgDto.setJsonData(signalLampManageService.getDeviceInfo(deviceId));
            } else if (deviceType == 3) {
                jsonMsgDto.setJsonData(signalMachineManageService.getDeviceInfo(deviceId));
            } else if (deviceType == 4) {
                jsonMsgDto.setJsonData(solarEnergyManageService.getDeviceInfo(deviceId));
            } else if (deviceType == 5) {
                jsonMsgDto.setJsonData(highLeverManageService.getDeviceInfo(deviceId));
            } else {
                jsonMsgDto.setJsonData(monitorService.getDeviceInfo(deviceId));
            }
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            jsonMsgDto.setIsSuccess(false);
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


}
